package xupt.se.ttms.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import xupt.se.ttms.idao.iEmployeeDAO;
import xupt.se.ttms.model.Employee;
import xupt.se.util.MyDBUtil;

public class EmployeeDAO implements iEmployeeDAO {
	
	private final String insertSql="insert into T_Employee(access,name,password,cName,tel)values(?,?,?,?,?)";
	//通过主键id 删除
	private final String deleteSql="delete from T_Employee where id=?";	
	
	private final String updateSql="update T_Employee set access=?,name=?,password=?,cName=?,tel=? where id=?";
	//通过主键id查询
	private final String selectSqlInt="select * from T_Employee where id=?";
	//通过姓名name查询
	private final String selectSqlStr="select * from T_Employee where access=?";
	//查询全部
	private final String selectSqlAll="select * from T_Employee ";

	/**
	 * 查询，传一个类型String,防sql注入攻击
	 * @param sql		sql语句
	 * @param str		where条件
	 * @return
	 * @throws Exception
	 */
	public ResultSet execQuery(String str) throws Exception {
		String	sql=selectSqlStr;
		ResultSet rstSet = null;
		MyDBUtil myDBUtil=new MyDBUtil();
		rstSet=myDBUtil.execQuery(sql, str);
		return rstSet;
	}
	/**
	 * 查询，传一个类型int,防sql注入攻击
	 * @param sql		sql语句
	 * @param i			where条件
	 * @return
	 * @throws Exception
	 */
	public ResultSet execQuery(int i) throws Exception {
		String	sql=selectSqlInt;
		ResultSet rstSet = null;
		MyDBUtil myDBUtil=new MyDBUtil();
		rstSet=myDBUtil.execQuery(sql, i);
		return rstSet;
	}	
	
	/**
	 * 删除 数据，传一个类型int，防sql注入攻击
	 * @param sql
	 * @param i
	 * @return
	 * @throws Exception
	 */
	public boolean execDelete(int i) throws Exception{
		String sql=deleteSql;
		boolean flag = false;
		MyDBUtil myDBUtil=new MyDBUtil();
		flag=myDBUtil.execDelete(sql, i);
		return flag;
	}
	@Override
	public List<Employee> HandleEmployeeResultSet(ResultSet rs) throws SQLException {
		 //结果，在第一个查询结果有个rs标记，通过rs.next()遍历获取全部
		Employee employee=null;
		List<Employee> employees=new ArrayList<>();
		 //获取结果的数据
        ResultSetMetaData rsmd = rs.getMetaData();
        //获取该条结果有多少个列数据
        int columnCount = rsmd.getColumnCount();
      
    	System.out.println("获取员工数据中");
    	  //通过rs.next()遍历获取全部结果
        while (rs.next()) {
        
        	employee=new Employee();
       	 //输出每条结果的全部数据
           for (int i = 0; i < columnCount; i++) {
        	   employee.setId(rs.getInt("id"));
        	   employee.setAccess(rs.getString("access"));	   
        	   employee.setName(rs.getString("name"));
        	   employee.setPassword(rs.getString("password"));
        	   employee.setcName(rs.getString("cName"));
        	   employee.setTel(rs.getString("tel"));
        	   
        	
               System.out.print(rs.getString(i+1) + "\t");

           }
           employees.add(employee);
           System.out.println();		                    
        }
    
        return employees;
	}
	@Override
	public ResultSet exexQueryAll() throws Exception {
		String	sql=selectSqlAll;
		ResultSet resultSet;
		MyDBUtil myDBUtil=new MyDBUtil();
		resultSet=myDBUtil.execAllQuery(sql);
		return resultSet;
	}
	@Override
	public boolean execInsert(Employee employee) throws Exception {
		String sql=insertSql;
		Connection conn=null;
		boolean flag=false;
		PreparedStatement ps=null;
		MyDBUtil db = new MyDBUtil();
		conn=db.openConnection();
		try {
			if(null==conn)
				throw new Exception("Database not connected!");
			
			ps=conn.prepareStatement(sql);
			//ps.setInt(1,employee.getAccess());
			ps.setString(1,employee.getAccess());
			ps.setString(2,employee.getName());
			ps.setString(3,employee.getPassword());
			ps.setString(4,employee.getcName());
			ps.setString(5,employee.getTel());

			flag=ps.execute();
			/*
			Statement stmt = conn.createStatement();
			flag = stmt.execute(sql);*/			
			ps.close();	
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}
	@Override
	public boolean execUpdate(Employee employee) throws Exception {
		String sql=updateSql;
		Connection conn=null;
		boolean flag=false;
		PreparedStatement ps=null;
		MyDBUtil db = new MyDBUtil();
		conn=db.openConnection();
		try {
			if(null==conn)
				throw new Exception("Database not connected!");
			
			ps=conn.prepareStatement(sql);
			//ps.setInt(1,employee.getAccess());
			ps.setString(1,employee.getAccess());
			ps.setString(2,employee.getName());
			ps.setString(3,employee.getPassword());
			ps.setString(4,employee.getcName());
			ps.setString(5,employee.getTel());
			ps.setInt(6, employee.getId());
			flag=ps.execute();
			/*
			Statement stmt = conn.createStatement();
			flag = stmt.execute(sql);*/			
			ps.close();	
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}
	
}
